package com.qf.java2201.controller;


import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.pojo.Orders;
import com.qf.java2201.service.OrdersService;
import com.qf.java2201.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@RestController
@RequestMapping("/orders")
@CrossOrigin(origins = {"http://127.0.0.1:5500"},allowCredentials = "true")

public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/ordersPageData")
    // 这里 的  方法内的  @RequestParam  不能省略    省略就无法获取参数了
    public PageVo<Orders> getPageData(@RequestParam Map<String,Object> params){
        return ordersService.getPageData(params);
    }


    @RequestMapping("/addOrders")
    public R addOrders(Orders orders){
        int result = ordersService.addOrders(orders);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("添加成功");
        }else{
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

    @RequestMapping("/updateOrders")
    public R updateStudent(Orders orders){
        // 更改学生信息
        int result = ordersService.updateOrders(orders);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("修改成功");
        }else{
            r.setCode(-1);
            r.setMsg("修改失败");
        }
        return r;
    }


    // 根据id 删除学生
    @RequestMapping("/deleteOrders")
    public R deleteOrders(Integer id){
        // 根据 id 删除学生信息
        int result = ordersService.deleteOrders(id);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("删除成功");
        }else{
            r.setCode(-1);
            r.setMsg("删除失败");
        }
        return r;
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        String path = OssUtil.uploadFile(inputStream,filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(path);
        return r;
    }

}
